package com.jl.jlapp.mvp.activity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.LayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter.OnItemClickListener;
import com.jl.jlapp.R;
import com.jl.jlapp.adapter.HotSearchAdapter;
import com.jl.jlapp.adapter.SearchHistoryWorldAdapter;
import com.jl.jlapp.db.DatabaseHelper;
import com.jl.jlapp.eneity.HotSearchWorld;
import com.jl.jlapp.nets.Api;
import com.jl.jlapp.nets.CommonObserver;
import com.jl.jlapp.nets.Net;
import com.jl.jlapp.popu.ClearHistorySearhPopu;
import com.jl.jlapp.utils.CustomGridLayoutManager;
import com.jl.jlapp.utils.CustomLanearLayoutManager;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

import java.util.ArrayList;
import java.util.List;

public class HomeSearchActivity extends AppCompatActivity implements OnClickListener {
    ClearHistorySearhPopu clearHistorySearhPopu;
    DatabaseHelper dbHelper;
    @BindView(R.id.has_history_search_world_linear)
    LinearLayout hasHistorySearchWorldLinear;
    HotSearchAdapter hotSearchAdapter;
    @BindView(R.id.hot_search_bottom_clear_history)
    LinearLayout hotSearchBottomClearHistory;
    @BindView(R.id.hot_search_btn)
    TextView hotSearchBtn;
    @BindView(R.id.hot_search_has_word_linear_layout)
    LinearLayout hotSearchHasWordLinearLayout;
    @BindView(R.id.hot_search_has_word_scroll_view)
    HorizontalScrollView hotSearchHasWordScrollView;
    HotSearchAdapter hotSearchHasWorldRecyclerViewAdapter;
    @BindView(R.id.hot_search_no_word_recycler_view)
    RecyclerView hotSearchRecyclerView;
    @BindView(R.id.hot_search_return_btn)
    ImageView hotSearchReturnBtn;
    List<String> hotWord;
    public OnClickListener itemclick = new OnClickListener() {
        public void onClick(View paramAnonymousView) {
            HomeSearchActivity.this.dbHelper.delete(HomeSearchActivity.this.sqliteDatabase);
            HomeSearchActivity.this.clearHistorySearhPopu.dismiss();
            HomeSearchActivity.this.searchHistoryWord.clear();
            HomeSearchActivity.this.setAdapter();
        }
    };
    private LayoutInflater mInflater;
    String oldSearchWorld = "";
    @BindView(R.id.search_history_recycler_view)
    RecyclerView searchHistoryRecyclerView;
    List<String> searchHistoryWord;
    SearchHistoryWorldAdapter searchHistoryWorldAdapter;
    @BindView(R.id.search_world)
    EditText searchWorld;
    SQLiteDatabase sqliteDatabase;
    View view;

    private void getSearchHistoryData() {
        Cursor localCursor = this.dbHelper.select(this.sqliteDatabase);
        if (this.searchHistoryWord == null) {
            this.searchHistoryWord = new ArrayList();
        }
        this.searchHistoryWord.clear();
        while (localCursor.moveToNext()) {
            this.searchHistoryWord.add(localCursor.getString(localCursor.getColumnIndex("name")));
        }
    }

    private void setAdapter() {
        if (this.hotWord.size() > 0) {
            if (this.hotSearchAdapter == null) {
                this.hotSearchRecyclerView.setLayoutManager(new CustomGridLayoutManager(this, 4, false));
                this.hotSearchAdapter = new HotSearchAdapter(R.layout.hot_search_item, this.hotWord);
                this.hotSearchRecyclerView.setAdapter(this.hotSearchAdapter);
                this.hotSearchAdapter.setOnItemClickListener(new OnItemClickListener() {
                    public void onItemClick(BaseQuickAdapter paramAnonymousBaseQuickAdapter, View paramAnonymousView, int paramAnonymousInt) {
                        TextView tvSearch = (TextView) paramAnonymousView.findViewById(R.id.hot_search_content);
                        toSearchActivity(tvSearch);
                    }
                });
            }
            this.hotSearchAdapter.notifyDataSetChanged();
        }

        if (this.searchHistoryWorldAdapter == null) {
            CustomLanearLayoutManager customLanearLayoutManager = new CustomLanearLayoutManager(this);
            ((CustomLanearLayoutManager) customLanearLayoutManager).setScrollEnabled(false);
            this.searchHistoryRecyclerView.setLayoutManager((LayoutManager) customLanearLayoutManager);
            this.searchHistoryWorldAdapter = new SearchHistoryWorldAdapter(R.layout.search_history_item, this.searchHistoryWord);
            this.searchHistoryRecyclerView.setAdapter(this.searchHistoryWorldAdapter);
            this.searchHistoryWorldAdapter.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(BaseQuickAdapter paramAnonymousBaseQuickAdapter, View paramAnonymousView, int paramAnonymousInt) {
                    TextView tvHistory = (TextView) paramAnonymousView.findViewById(R.id.search_history_world);
                    toSearchActivity(tvHistory);

                }
            });
        } else {
            this.searchHistoryWorldAdapter.notifyDataSetChanged();
        }

    }

    private void toSearchActivity(TextView tvContent) {
        Intent intent = new Intent(HomeSearchActivity.this, SearchGoodsListActivity.class);
        intent.putExtra("searchMsg", tvContent.getText().toString().trim());
        HomeSearchActivity.this.startActivity(intent);
    }

    private void setListener() {
        this.hotSearchBottomClearHistory.setOnClickListener(this);
        this.hotSearchBtn.setOnClickListener(this);
        this.hotSearchReturnBtn.setOnClickListener(this);
    }

    public void onClick(View paramView) {
        switch (paramView.getId()) {
            case R.id.hot_search_bottom_clear_history:
                this.clearHistorySearhPopu = new ClearHistorySearhPopu(this, this.itemclick, Integer.valueOf(0));
                this.clearHistorySearhPopu.showAtLocation(findViewById(R.id.home_search_page), 17, 0, 0);
                break;
            case R.id.hot_search_btn:
                String worldStr = this.searchWorld.getText().toString().trim();
                if (!"".equals(worldStr)) {
                    for (int i = 0; i < searchHistoryWord.size(); i++) {
                        if (searchHistoryWord.get(i).equals(worldStr)) {
                            this.dbHelper.add(this.sqliteDatabase, worldStr);
                            getSearchHistoryData();
                        }
                    }
                    if (this.searchHistoryWorldAdapter == null) {
                        CustomLanearLayoutManager customLanearLayoutManager = new CustomLanearLayoutManager(this);
                        customLanearLayoutManager.setScrollEnabled(false);
                        this.searchHistoryRecyclerView.setLayoutManager(customLanearLayoutManager);
                        this.searchHistoryWorldAdapter = new SearchHistoryWorldAdapter(R.layout.search_history_item, this.searchHistoryWord);
                        this.searchHistoryRecyclerView.setAdapter(this.searchHistoryWorldAdapter);
                        this.searchHistoryWorldAdapter.setOnItemClickListener(new OnItemClickListener() {
                            public void onItemClick(BaseQuickAdapter paramAnonymousBaseQuickAdapter, View paramAnonymousView, int paramAnonymousInt) {
                                TextView tvHistory = (TextView) paramAnonymousView.findViewById(R.id.search_history_world);
                                toSearchActivity(tvHistory);
                            }
                        });
                    } else {
                        this.searchHistoryWorldAdapter.notifyDataSetChanged();
                    }
                    Intent intent = new Intent(HomeSearchActivity.this, SearchGoodsListActivity.class);
                    intent.putExtra("searchMsg", worldStr);
                    HomeSearchActivity.this.startActivity(intent);
                    finish();
                }
                break;
        }
    }

    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(R.layout.activity_home_search);
        ButterKnife.bind(this);
        this.mInflater = LayoutInflater.from(this);
        this.oldSearchWorld = getIntent().getStringExtra("oldSearchWorld");
        this.searchWorld.setText(this.oldSearchWorld);
        if (this.dbHelper == null) {
            this.dbHelper = new DatabaseHelper(this);
            this.sqliteDatabase = this.dbHelper.getWritableDatabase();
        }
        setListener();
        getSearchHistoryData();
        selectHotSearchWord();
    }

    public void selectHotSearchWord() {
        Net.get().selectHotSearchWord().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CommonObserver<HotSearchWorld>() {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(HotSearchWorld hotSearchWorld) {
                        if (hotSearchWorld.getResultData() != null) {
                            searchHistoryWord = hotSearchWorld.getResultData();
                            setAdapter();
                        }
                    }
                });
    }
}


/* Location:              /Users/davidzhang/Documents/decompileApk/jlfood-dex2jar.jar!/com/jl/jlapp/mvp/activity/HomeSearchActivity.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */